package com.wechat.weibo.controller;

import com.wechat.weibo.entity.User;
import com.wechat.weibo.entity.WeiBo;
import com.wechat.weibo.service.LoginService;
import com.wechat.weibo.service.WeiboService;
import com.wechat.weibo.util.ResultMap;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Api
@Controller
public class WeiBoController {
    @Autowired
    WeiboService weiboService;
    @Autowired
    LoginService loginService;

    @ApiOperation(value = "删除微博")
    @GetMapping("/delWeibo")//ok
    @ResponseBody
    public ResultMap delWeibo(String selfID,Integer weibo_id){
        User user=loginService.getUser(selfID);
        WeiBo weiBo=weiboService.showSingle(selfID,weibo_id);
        if(user==null||weiBo==null)return ResultMap.wrong();
        if(!(weiBo.getHost_openid().equals(user.getOpenid())))return new ResultMap(101,"you are not the host",null);
        weiboService.delWeibo(user,weibo_id);
        return ResultMap.success(null);
    }
//    微博详情页
    @ApiOperation(value = "获取单条微博信息")
    @GetMapping("/showSingleWeibo")//ok
    @ResponseBody
    public ResultMap getSingle(String selfID,Integer weibo_id){
//        System.out.print(selfID+" !Ones");
        if(weibo_id==null)return ResultMap.wrong();
        if(loginService.getUser(selfID)==null)
            return new ResultMap(200,"your havent login",weiboService.showSingle(selfID,weibo_id));
        return ResultMap.success(weiboService.showSingle(selfID,weibo_id));
    }

//    发微博
    @ApiOperation(value = "发微博")
    @PostMapping("/postWeibo")
    @ResponseBody
    public ResultMap postWeibo(String selfID,String content){
//        System.out.print(selfID+"a"+content+"b\n");
        if(selfID==null||content==null)return ResultMap.wrong();
        User user=loginService.getUser(selfID);
        if(user==null)return ResultMap.wrong();
        weiboService.postWeibo(user,content);
        return ResultMap.success(null);
    }
//    个人主页微博
    @ApiOperation(value = "获取某人的所有微博")
    @GetMapping("/showOnesWeibo")//ok
    @ResponseBody
    public ResultMap getOnesWei(String selfID,String otherID){
//        System.out.print(selfID+" !Ones");
        if(otherID==null)return ResultMap.wrong();
        if(loginService.getUser(selfID)==null)
            return new ResultMap(200,"your havent login",weiboService.getWeiBo(selfID,otherID));
        return ResultMap.success(weiboService.getWeiBo(selfID,otherID));
    }
//    广场微博
    @ApiOperation(value = "获取所有人的微博")
    @GetMapping("/showAllWeibo")//ok
    @ResponseBody
    public ResultMap getOnesWei(String selfID){
//        if(selfID==null)return ResultMap.wrong();
//        System.out.print(selfID+" !all");
        if(loginService.getUser(selfID)==null)
            return new ResultMap(200,"your havent login",weiboService.getAllWeiBo(selfID));
        return ResultMap.success(weiboService.getAllWeiBo(selfID));
    }
//    所有关注人微博
    @ApiOperation(value = "获取所有关注人的微博 包括自己")
    @GetMapping("/showFollowWeibo")//ok
    @ResponseBody
    public ResultMap getFollowWei(String selfID){
//        System.out.print(selfID+" !follow");
        if(loginService.getUser(selfID)==null)return ResultMap.wrong();
        return ResultMap.success(weiboService.getFollowWeiBo(selfID));
    }

//    点赞
    @ApiOperation(value = "点赞某微博")
    @GetMapping("/addLike")
    @ResponseBody
    public ResultMap addLike(String openid,Integer weibo_id){
        if(openid==null||weibo_id==null)return ResultMap.wrong();
        if(loginService.getUser(openid)==null)return ResultMap.wrong();
        int tmp=weiboService.addLike(openid,weibo_id);
        if(tmp==0)return ResultMap.success(null);
        if(tmp==1)return new ResultMap(103,"missing weibo",null);
        else return new ResultMap(102,"reLike",null);
    }
//    取消赞
    @ApiOperation(value = "取消某微博赞")
    @GetMapping("/delLike")
    @ResponseBody
    public ResultMap delLike(String openid,Integer weibo_id){
        if(openid==null||weibo_id==null)return ResultMap.wrong();
        if(loginService.getUser(openid)==null)return ResultMap.wrong();
        int tmp=weiboService.delLike(openid,weibo_id);
        if(tmp==0)return ResultMap.success(null);
        if(tmp==1)return new ResultMap(103,"missing weibo",null);
        else return new ResultMap(102,"not like already",null);
    }
}
